package com.linkq.ipinyin.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linkq.core.base.BaseService;
import com.linkq.ipinyin.dao.TrailDetailDao;
import com.linkq.ipinyin.model.TrailDetail;
import com.linkq.ipinyin.vo.ResultInfo;
import org.springframework.stereotype.Service;

/**
 * @author wangpeng
 * @since 2025-07-12
 **/

@Service
public class TrailDetailService extends BaseService<TrailDetailDao, TrailDetail> {

    public TrailDetail newTrail(TrailDetail trailDetail) {
        TrailDetail original = selectOne(
                Wrappers.<TrailDetail>lambdaQuery()
                        .eq(TrailDetail::getCustomerId, trailDetail.getCustomerId())
                        .eq(TrailDetail::getTrailType, trailDetail.getTrailType())
        );
        if (original == null) {
            trailDetail.setCount(1);
            insert(trailDetail);
            return trailDetail;
        }
        original.setCount(original.getCount() + 1);
        updateById(original);
        return original;
    }

    public int getCount(TrailDetail trailDetail) {
        TrailDetail original = selectOne(
                Wrappers.<TrailDetail>lambdaQuery()
                        .eq(TrailDetail::getCustomerId, trailDetail.getCustomerId())
                        .eq(TrailDetail::getTrailType, trailDetail.getTrailType())
        );
        if (original == null) {
            return 0;
        }
        return original.getCount();
    }
}
